package com.iqiyi.pps.epg.web.utils;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: wayne
 * Date: 12-6-18
 * Time: 下午3:52
 */
public class JxlTools
{
    private Workbook workbook   = null; // 工作部对象

    private HashMap<String, String> mapData    = null; // data数据

    private Sheet sheet = null; // 工作表

    public int  totalRows  = 0;   // 总行数

    public int totalCells = 0;   // 总列数

    public int getSheetNum() {
        return sheetNum;
    }

    private int sheetNum = 0;

    /**
     * 以一个InputStream为参数的构造器
     *
     * @param inputStream
     * @throws java.io.IOException
     * @throws jxl.read.biff.BiffException
     */
    public JxlTools(InputStream inputStream) throws BiffException, IOException
    {
        this.workbook = Workbook.getWorkbook(inputStream);
        this.sheet = this.workbook.getSheet(0);
        this.sheetNum = this.workbook.getNumberOfSheets();
        this.getRows();
        this.getCells();
    }

    /**
     * 以一个File为参数的构造器
     *
     * @param file
     * @throws java.io.IOException
     * @throws jxl.read.biff.BiffException
     */
    public JxlTools(File file) throws BiffException, IOException
    {
        this(new FileInputStream(file));
    }

    /**
     * 以一个文件路径path的构造器
     *
     * @param filePath
     * @throws java.io.IOException
     * @throws jxl.read.biff.BiffException
     */
    public JxlTools(String filePath) throws BiffException, IOException
    {

        this(new File(filePath));
    }

    /**
     * 把所有数据放到一个map中去,key为行号加列号
     *
     * @return
     */
    public HashMap<String, String> getExcelDate()
    {
        mapData = new HashMap<String, String>();
        for (int i = 0; i < this.totalRows; i++)
        {
            for (int j = 0; j < this.totalCells; j++)
            {
                this.mapData.put(i + "" + j, this.getData(j, i));
            }
        }
        return this.mapData;
    }

    /**
     * 把所有数据放到一个map中去,key为行号加列号
     *
     * @return
     */
    public Map<String, List> getExcelDateForRows()
    {
        Map<String,List> mapDataForRow = new HashMap<String, List>();
        for (int i = 0; i < this.totalRows; i++)
        {
            List list = new LinkedList();
            for (int j = 0; j < this.totalCells; j++)
            {
                list.add(this.getData(j, i));
            }
            mapDataForRow.put(""+i,list);
        }
        return mapDataForRow;
    }

    /**
     * 把所有数据放到一个map中去,key为行号加列号
     *
     * @return
     */
    public Map<String, List> getExcelDateForRows(int index)
    {
        sheet = this.workbook.getSheet(index);
        this.getRows();
        this.getCells();
        Map<String,List> mapDataForRow = new HashMap<String, List>();
        for (int i = 0; i < this.totalRows; i++)
        {
            List list = new LinkedList();
            for (int j = 0; j < this.totalCells; j++)
            {
                list.add(this.getData(j, i));
            }
            mapDataForRow.put(""+i,list);
        }
        return mapDataForRow;
    }

    /**
     * 得到总行数
     */
    private void getRows()
    {
        this.totalRows = sheet.getRows();
    }

    /**
     * 得到总列数
     */
    private void getCells()
    {
        this.totalCells = this.sheet.getColumns();
    }

    /**
     * 按行得到数据
     *
     * @param cell
     * @param row
     * @return
     */
    private String getData(int cell, int row)
    {
        Cell rs = this.sheet.getCell(cell, row);
        return rs.getContents();
    }
}
